﻿using DAO.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class DaoFaccade
    {
        DataManager dm;
        public DaoFaccade()
        {
            dm = new DataManager();
        }

        /// <summary>
        /// Метод сохранения сервера
        /// </summary>
        /// <param name="entity">сохраняемая сущность</param>
        public void AddNewServerObject(Server entity)
        {
            int savedId = 0;
            int objectType = 0;

            List<DeployObject> depObjects = new List<DeployObject>();
            entity.DeployObjects.ToList().ForEach(item =>
                {
                    depObjects.Add(item);
                });
            entity.DeployObjects = null;
            dm.ServerManager.Add((Server)entity);
            dm.SaveChanges();
            savedId = entity.id;
            objectType = (int)ObjectTypesEnum.Server;

            if (savedId == 0)
                throw new InvalidOperationException("Ошибка при сохранении операции. Нулевой идентификатор объекта");

            var operation = new Operation
            {
                IdObject = savedId,
                IdObjectType = objectType,
                IdKindOperationType = (int)KindOperationsEnum.Add,
                DateOperation = DateTime.Now
            };
            dm.OperationManager.Add(operation);
            dm.SaveChanges();
            entity.DeployObjects = depObjects.ToList();
            if (entity.DeployObjects != null)
            {
                foreach (var item in entity.DeployObjects)
                {
                    if (item.IdServer == 0)
                        item.IdServer = entity.id;
                    AddNewServerObject(item);
                }
            }
        }

         /// <summary>
         /// Метод сохранения объекта сервера
         /// </summary>
         /// <param name="entity"></param>
        public void AddNewServerObject(DeployObject entity)
        {
            int savedId = 0;
            dm.ServerObjectsManager.Add(entity);
            dm.SaveChanges();
            savedId = entity.id;
            if (savedId == 0)
                throw new InvalidOperationException("Ошибка при сохранении операции. Нулевой идентификатор объекта");
            var operation = new Operation
            {
                IdObject = savedId,
                IdObjectType = (int)ObjectTypesEnum.ServerObject,
                IdKindOperationType = (int)KindOperationsEnum.Add,
                DateOperation = DateTime.Now,
                UserName = WindowsIdentity.GetCurrent().Name
            };
            dm.OperationManager.Add(operation);
            dm.SaveChanges();
        }

        public void UpdateDeployObject(DeployObject entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");
            if (entity.BuildParameter != null)
            {
                dm.BuildParametersManager.Edit(entity.BuildParameter);
            }
            if (entity.Customization != null)
            {
                dm.CustomizationManager.Edit(entity.Customization);
            }
            if (entity.DeployParameter != null)
            {
                dm.DeployParametersManager.Edit(entity.DeployParameter);
            }
            if (entity.SvnParameter != null)
            {
                dm.SvnParametersManager.Edit(entity.SvnParameter);
            }

            var obj = dm.ServerObjectsManager.GetById(entity.id);
            if (obj.NotificationsApplications != null && obj.NotificationsApplications.Count > 0)
            {
                List<int> objNots = new List<int>();
                obj.NotificationsApplications.ToList().ForEach
                    (t =>
                    { 
                        objNots.Add(t.id);
                    });

                objNots.ForEach
                    (t =>
                        {
                            dm.NotificationsApplicationsManager.Delete(t);
                        }
                    );
                dm.SaveChanges();
            }
            if (entity.NotificationsApplications != null && entity.NotificationsApplications.Count > 0)
            {
                foreach (var item in entity.NotificationsApplications)
                {
                    dm.NotificationsApplicationsManager.Add(item);
                }
            }
            dm.ServerObjectsManager.Edit(entity);
            var operation = new Operation
            {
                DateOperation = DateTime.Now,
                IdKindOperationType = (int)KindOperationsEnum.Edit,
                IdObject = entity.id,
                IdObjectType = (int)ObjectTypesEnum.ServerObject,
                UserName = WindowsIdentity.GetCurrent().Name
            };
            dm.OperationManager.Add(operation);
            dm.SaveChanges();
        }
    }
}
